package it.polimi.it.TravelDream.ejb.eaos;

import it.polimi.it.TravelDream.entities.PersonalizedPackage;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class PersPackageMgrBean
 */
@Stateless
@LocalBean
public class PersPackageMgrBean implements PersPackageMgr {
	
	@PersistenceContext
    private EntityManager em;
   
	
	//crea un nuovo pacchetto personalizzato e lo memorizza nel database
	public String personalize(PackageDTO pack, String user){
		int numberOfPackages = (em.createQuery(
				"SELECT p FROM PersonalizedPackage p WHERE p.usercreator LIKE :user").setParameter("user", user).getResultList().size());
		if(numberOfPackages < 5){
		System.out.println("***************************");
		System.out.println(user);
		PersonalizedPackage persPackage = new PersonalizedPackage(pack,user);
		em.persist(persPackage);
		return " Ok ";
		}else{
			return " Sorry your personalized Package List is full!!!";
		}
	}
	
	//va ad aggiornare nel database il pacchetto personalizzato
	public void changeBasicProduct(PersonalizePackageDTO persPack, String userCreator ){
		System.out.println("*************************");
		System.out.println(persPack.getIdbasicproduct1());
		System.out.println(userCreator);
		System.out.println("*************************");
			em.merge(new PersonalizedPackage(persPack, userCreator));
		}
				 
	
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<PersonalizePackageDTO> getPersPackList(String user){
			System.out.println("*************************");
			return listConverterDTO(em.createQuery(
					"SELECT p FROM PersonalizedPackage p WHERE p.usercreator LIKE :user").setParameter("user", user).getResultList());
					 //WHERE p.usercreator LIKE :user").setParameter("user", user).getResultList());
		}	
		
	
	private ArrayList<PersonalizePackageDTO> listConverterDTO(List<PersonalizedPackage> persPackList){
		
		int i;
		ArrayList<PersonalizePackageDTO> newList = new ArrayList<PersonalizePackageDTO>();
		for( i=0;i<persPackList.size();i++){
			newList.add(convertToDTO(persPackList.get(i)));
		}
		
		return newList;
			
		}
			
		
		private PersonalizePackageDTO convertToDTO(PersonalizedPackage persPackage){
			PersonalizePackageDTO persPackDTO = new PersonalizePackageDTO();
			persPackDTO.setCreationdata(persPackage.getCreationdata());
			persPackDTO.setIdbasicproduct1(persPackage.getIdbasicproduct1());
			persPackDTO.setIdbasicproduct2(persPackage.getIdbasicproduct2());
			persPackDTO.setIdbasicproduct3(persPackage.getIdbasicproduct3());
			persPackDTO.setIdpackage(persPackage.getIdpackage());
			persPackDTO.setTotalprice(persPackage.getTotalprice());
			persPackDTO.setUsercreator(persPackage.getUsercreator());
			
			
			return persPackDTO;
		}
		
		

    /**
     * Default constructor. 
     */
    public PersPackageMgrBean() {
        // TODO Auto-generated constructor stub
    }

}
